

# Create your views here.
from django.db.migrations import serializer
from django.shortcuts import render

# Create your views here.
from rest_framework.decorators import action
from rest_framework.generics import ListAPIView, GenericAPIView, CreateAPIView
from rest_framework.mixins import RetrieveModelMixin
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ReadOnlyModelViewSet

from tweets.models import Tweet, Comment
from tweets.serializers import TweetListSerializer, CommentSerializer, TweetDetailSerializer, CreateTweetSerializer


class TweetResultsSetPagination(PageNumberPagination):
    page_size = 5
    page_size_query_param = 'page_size'
    max_page_size = 5


class TweetViewSet(ReadOnlyModelViewSet):
    pagination_class = TweetResultsSetPagination

    def get_queryset(self):
        if self.action == 'list':

            return Tweet.objects.filter(is_delete=False).order_by('-create_time')
        else:

            return Tweet.objects.all().order_by('-create_time')

    def get_serializer_class(self):
        if self.action == 'list':
            return TweetListSerializer
        else:
            return TweetDetailSerializer


class SubmitView(CreateAPIView):
    serializer_class = CreateTweetSerializer



# class TweetView(ListAPIView):
#     queryset = Tweet.objects.filter(is_delete=False).order_by('-create_time')
#     serializer_class = TweetListSerializer
#
#
# class CommentViwe(ListAPIView):
#     def get_queryset(self):
#         s = self.kwargs['pk']
#         print(s)
#         # return Comment.objects.filter(is_delete=False).order_by('-create_time')
#         c = Comment.objects.filter(tweet_id=s).order_by('-create_time')
#         print(c)
#         return Comment.objects.filter(tweet_id=s).order_by('-create_time')
#     serializer_class = CommentSerializer
#

#
# class TweetViewSet(ReadOnlyModelViewSet):
#
#     serializer_class = TweetListSerializer
#
#     @action(methods=['get'], detail=False)
#     def one(self, request, pk):
#         tweet = self.get_object()
#         a = tweet.tweets_comment
#         serializer = self.get_serializer(tweet)
#         c = serializer.data
#
#         return Response(c)
#
#
#     def get_queryset(self):
#
#         if self.action == 'list':
#
#             return Tweet.objects.filter(is_delete=False).order_by('-create_time')
#         elif self.action == 'one':
#             id = self.kwargs['pk']
#             print(id)
#             return Tweet.objects.filter(id=id).order_by('-create_time')

